/*
package com.wad.interceptors;

import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wad.utlis.JWTUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

*/
/**
 * @author wad
 * @date 2022/11/11 11:20
 * @project JWT_DEMO2
 **//*

public class JWTInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        Map<String,Object> map=new HashMap<>();
        try {
            JWTUtil.verifyToken(token);
//            验证成功
            return true;
        }catch (TokenExpiredException e){
            //过期异常
            map.put("state",false);
            map.put("msg","token已过期");
        }catch (SignatureVerificationException e){
            //签名不一致异常
            map.put("state",false);
            map.put("msg","签名不一致");
        }catch (AlgorithmMismatchException e){
//            算法不匹配异常
            map.put("state",false);
            map.put("msg","算法不匹配");
        }catch (Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","token无效");
        }
        String json=new ObjectMapper().writeValueAsString(map);//将map转为json字符串
        response.setContentType("application/json;charset=utf-8");//设置响应格式
        response.getWriter().println(json);
        return false;
    }
}
*/
